# Contributor: Pedro Filipe <xpecex@outlook.com>
# Contributor: Leonardo Arena <rnalrd@alpinelinux.org>
# Contributor: V.Krishn <vkrishn4@gmail.com>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=varnish
pkgver=6.4.0
pkgrel=0
pkgdesc="High-performance HTTP accelerator"
url="https://www.varnish-cache.org/"
arch="all"
license="BSD-2-Clause"
options="!check" # tests fail
depends="gcc libc-dev libgcc"
makedepends="pcre-dev ncurses-dev libedit-dev py3-docutils
	linux-headers libunwind-dev py3-sphinx"
install="$pkgname.pre-install"
subpackages="$pkgname-dbg $pkgname-dev $pkgname-doc $pkgname-libs $pkgname-geoip::noarch $pkgname-openrc"
pkgusers="varnish"
pkggroups="varnish"
source="https://varnish-cache.org/_downloads/varnish-$pkgver.tgz
	musl-include-vpf.patch
	musl-include-vsb.patch
	varnishd.initd
	varnishd.confd
	varnishlog.initd
	varnishlog.confd
	varnishncsa.initd
	varnishncsa.confd
	varnishd.logrotate
	maxminddb.vcl"


# secfixes:
#   6.2.1-r0:
#     - CVE-2019-15892
#   5.2.1-r0:
#     - CVE-2017-8807
#   5.1.3-r0:
#     - CVE-2017-12425


build() {
	./configure \
		--build="$CBUILD" \
		--host="$CHOST" \
		--prefix=/usr \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstatedir=/var/lib \
		--with-unwind \
		--without-jemalloc
	make
}

check() {
	make -j1 check || {
		find . -name 'test-suite.log' -print -exec cat '{}' \;
		return 1
	}
}

package() {
	make DESTDIR="$pkgdir" install

	local i; for i in $source; do
		case $i in
		*.initd) install -Dm755 "$srcdir/$i" \
				"$pkgdir/etc/init.d/${i%.*}" ;;
		*.confd) install -Dm644 "$srcdir/$i" \
				"$pkgdir/etc/conf.d/${i%.*}" ;;
		esac
	done
	install -Dm644 "$srcdir"/varnishd.logrotate \
		"$pkgdir"/etc/logrotate.d/varnishd
	install -d -o varnish -g varnish -m750 \
		"$pkgdir"/var/cache/varnish \
		"$pkgdir"/var/log/varnish \
		"$pkgdir"/var/lib/varnish
	install -d -o root -g varnish -m750 "$pkgdir"/etc/varnish
}

libs() {
	pkgdesc="Libraries for varnish"

	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/*.so.* "$subpkgdir"/usr/lib/
}

geoip() {
	pkgdesc="Maxmind GeoIP2 lookup plugin for varnish"
	depends="libmaxminddb-dev"

	install -m755 -D "$srcdir"/maxminddb.vcl \
		"$subpkgdir"/usr/lib/varnish/plugins/maxminddb.vcl
}

sha512sums="cda8f9e1d301a2b79db14685a23e25e36225f37065a1b7f37c5ae12fbb0483be51be9ffcc8ba72c1f65f5a022d1e408825694daed6780e206b9ba91feb2a07a1  varnish-6.4.0.tgz
2123668169b055f2d88f9b5b8e0877ca8b3cbfcd61e03d91fd7d0513b3267e4ef01a4d858cc6a3298cca0a49aaea2f92ff4fd9c0baf52a6c67b452a53f7e54d0  musl-include-vpf.patch
c51c8964880990c2b01807b2a38d886b146736a918bda9ea2e032c50085bf6745cab3cccb4ee4c561ab936a8b7cfb278cfcb758543ea6c605c15b8973c9f10ce  musl-include-vsb.patch
5ac7867e85cbd721f903c524ed4b524423d9dada4acfeefb0e543214a208828df5cc4efe2f012991bea6b38c2b223c24b17d3890ec4ed2c57d2b441b8e5a6900  varnishd.initd
98c8901e53f3c6adb908b9eb01fd441ea53f3c54d4a79aecb1fb1eddfa2fc93de3d0de403a4464a9baff15873b3a20623837e3a37cb225e924a329e32b9e37d6  varnishd.confd
c67a7898f40849989edcbe74e1a418e196f48e178ca30126bc13db226e4f4c2c2ba74d9d0650ef68ed88732917fc06b80ef1f62c2d9d2c14a89f26fd25aed0f7  varnishlog.initd
88fe9b545a2b25d6e6e45f1cfecbeca5f503bc1fbe95856386e568edee41b487b162cddceba8cb152b0a37d8d96635a76c0715fc69d87d6c667ac403ebae0bb7  varnishlog.confd
e0b7d67bbd710f0a17b77837c581f128e6b746eff2b12e81d03d1ad040037e95bb00fb8007d89bc6dab18cfa659456078f310ac268f104774ef85ad068efecca  varnishncsa.initd
a5426ff66b89d2afb6273f05e4117b3eec5ce0162a624d52c92b418960f72e58bd01224165613221af76ec241bd98e1eb985b2ef7b83a5b615e9ece67234dcc8  varnishncsa.confd
51cc6d46ff7439de93977ab87dfb0af399458c1e446475696f73342ae7a0c1a8ca8fc6e79e593659f1af30716a5f8a1ee5e3b1f5e7b35df40b45d47e7b0f2ffd  varnishd.logrotate
69f088819cff6d4441813be284f4117f232d08908515bd15d96bd5bb9d41ba7100657a52fd408d44c396d004366062ae22fbf08e2a983cd8023b554539ccf596  maxminddb.vcl"
